# coding=utf8
import sys
from flask import Flask
import datetime
import os
from flask import Flask, request, redirect, render_template
import paramiko
from flask import Flask, render_template, redirect, url_for, request, session
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import or_, func, and_, distinct

app = Flask(__name__)
db = SQLAlchemy(app)
app.config['SECRET_KEY'] = os.urandom(24)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@127.0.0.1:3306/hy'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True


class datail(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(20))
    type = db.Column(db.String(20))
    datetime = db.Column(db.DateTime, default=datetime.datetime.now)
    Amounts = db.Column(db.Integer)  # 数量
    price = db.Column(db.Integer)  # 单价
    Total = db.Column(db.Integer)  # 总计价格


class students(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(50))
    status = db.Column(db.String(100))
    projects = db.Column(db.String(100))  # 所学项目
    tuition = db.Column(db.Integer)  # 学费
    productsales = db.Column(db.Integer)  # 产品购买
    teacher = db.Column(db.String(100))  # 招生老师
    sources = db.Column(db.String(100))  # 学员来自


class Total(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    totalincome = db.Column(db.Integer)  # 总计收入金额
    totalexpend = db.Column(db.Integer)  # 总计支出金额
    balance = db.Column(db.Integer)  # 剩余金额


@app.route('/students', methods=['POST', 'GET'])
def students():
    if request.method == 'GET':
        return render_template('student.html')


@app.route('/index', methods=['POST', 'GET'])
def index():
    if request.method == 'GET':
        db.create_all()
        return render_template('index.html')
    else:
        Amount = request.form.get('Amount')
        pri = request.form.get('price')
        addtotaldata = int(Amount) * int(pri)
        types = request.form.get('type')
        if types == "收入":
            fromdata = request.form
            adddata1 = datail(name=fromdata['name'], Amounts=fromdata['Amount'], price=fromdata['price'],
                              type=fromdata['type'], Total=addtotaldata)
            db.session.add(adddata1)
            db.session.commit()
            result1 = Total.query.first()
            upd = 20000
            result1.totalincome = upd
            db.session.commit
            totals = result1.totalincome
            uptotalin = int(totals) + int(addtotaldata)
            result1.totalincome = uptotalin
            db.session.commit
        else:
            print("支出")
        return 'Hello World!'


if __name__ == '__main__':
    app.run()
